function bluetoothScan() {
    var result = "";
    ble.startScanWithOptions([],
        {reportDuplicates: false},
        function(device) {
            if(result.indexOf(device.id) < 0) {
                result += device.name;
                result += device.id;
                result += "<br />";
                document.getElementById("bleInfo").innerHTML = result;
            }
        },
        function(info){
            document.getElementById("bleInfo").innerHTML = "扫描失败";
        }
    );
}

function stopScan() {
    ble.stopScan(function(){
        document.getElementById("stopScanInfo").innerHTML = "关闭扫描";
    },function(){
        document.getElementById("stopScanInfo").innerHTML = "关闭失败";
    });
}

function getUuids() {
    var result = "";
    var address = "F6:AC:52:47:E8:72";//测试使用的，实际以开发者的连接的蓝牙为准
    bluetooth.getUuids(function(device){
        for(var i=0; i<device.uuids.length; i++) {
            result += device.uuids[i];
            result += "<br />";
            document.getElementById("uuidsInfo").innerHTML = result;
        }
    },function(){
        document.getElementById("uuidsInfo").innerHTML = "获取失败";
    }, address);
}

function bluetoothConnect() {
    var deviceUUId = "00001101-0000-1000-8000-00805f9b34fb"; //串口打印uuid，实际以开发者的连接的蓝牙为准
    var address = "F6:AC:52:47:E8:72";//测试使用的，实际以开发者的连接的蓝牙为准
    bluetooth.connect(function(){
        document.getElementById("connectInfo").innerHTML = "连接成功";
    },function(errorInfo){
        document.getElementById("connectInfo").innerHTML = "连接失败";
    },{
        uuid: deviceUUId,
        address: address
    });
}

function isBluetoothConnect() {
    bluetooth.isConnected(function(){
        document.getElementById("isConnectInfo").innerHTML = "连接成功";
    },function(){
        document.getElementById("isConnectInfo").innerHTML = "未连接";
    });
}

function isBluetoothConnectManage() {
    bluetooth.isConnectionManaged(function(isConnected){
        if(isConnected) {
            document.getElementById("isConnectManageInfo").innerHTML = "连接成功";
        } else {
            document.getElementById("isConnectManageInfo").innerHTML = "未连接";
        }
    },
        function(){
            document.getElementById("isConnectManageInfo").innerHTML = "打印机已经失联";
        });
}

function _EscCommand(isHarmony) {
    this.ESC = "\u001B";
    this.GS = "\u001D";
    this.InitializePrinter = this.ESC + "@";
    this.BoldOn = this.ESC + "E" + "\u0001";
    this.BoldOff = this.ESC + "E" + "\0";
    if(isHarmony)
        this.BoldOff = this.ESC + "E" + "#";
    this.DoubleHeight = this.GS + "!" + "\u0001";
    this.DoubleWidth = this.GS + "!" + "\u0010";
    this.DoubleOn = this.GS + "!" + "\u0011"; // 2x sized text (double-high + double-wide)
    this.DoubleOff = this.GS + "!" + "\0";
    if(isHarmony)
        this.DoubleOff = this.GS + "!" + "#";
    this.PrintAndFeedMaxLine = this.ESC + "J" + "\u00FF"; // 打印并走纸 最大255
    this.TextAlignLeft = this.ESC + "a" + "0";
    this.TextAlignCenter = this.ESC + "a" + "1";
    this.TextAlignRight = this.ESC + "a" + "2";
    this.CutAllPage = this.ESC + "i";
    this.CutHalfPage = this.ESC + "m";
}

function GB2312UTF8(){
    this.Dig2Dec=function(s){
        var retV = 0;
        if(s.length == 4){
            for(var i = 0; i < 4; i ++){
                retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
            }
            return retV;
        }
        return -1;
    }

    this.Hex2Utf8=function(s){
        var retS = "";
        var tempS = "";
        var ss = "";
        if(s.length == 16){
            tempS = "1110" + s.substring(0, 4);
            tempS += "10" + s.substring(4, 10);
            tempS += "10" + s.substring(10,16);
            var sss = "0123456789ABCDEF";
            for(var i = 0; i < 3; i ++){
                retS += "%";
                ss = tempS.substring(i * 8, (eval(i)+1)*8);
                retS += sss.charAt(this.Dig2Dec(ss.substring(0,4)));
                retS += sss.charAt(this.Dig2Dec(ss.substring(4,8)));
            }
            return retS;
        }
        return "";
    }

    this.Dec2Dig=function(n1){
        var s = "";
        var n2 = 0;
        for(var i = 0; i < 4; i++){
            n2 = Math.pow(2,3 - i);
            if(n1 >= n2){
                s += '1';
                n1 = n1 - n2;
            }
            else
                s += '0';
        }
        return s;
    }

    this.Str2Hex=function(s){
        var c = "";
        var n;
        var ss = "0123456789ABCDEF";
        var digS = "";
        for(var i = 0; i < s.length; i ++){
            c = s.charAt(i);
            n = ss.indexOf(c);
            digS += this.Dec2Dig(eval(n));
        }
        return digS;
    }

    this.Gb2312ToUtf8=function(s1){
        var s = escape(s1);
        var sa = s.split("%");
        var retV ="";
        if(sa[0] != ""){
            retV = sa[0];
        }
        for(var i = 1; i < sa.length; i ++){
            if(sa[i].substring(0,1) == "u"){
                retV += this.Hex2Utf8(this.Str2Hex(sa[i].substring(1,5)));
                if(sa[i].length){
                    retV += sa[i].substring(5);
                }
            }
            else{
                retV += unescape("%" + sa[i]);
                if(sa[i].length){
                    retV += sa[i].substring(5);
                }
            }
        }
        return retV;
    }

    this.Utf8ToGb2312=function(str1){
        var substr = "";
        var a = "";
        var b = "";
        var c = "";
        var i = -1;
        i = str1.indexOf("%");
        if(i==-1){
            return str1;
        }
        while(i!= -1){
            if(i<3){
                substr = substr + str1.substr(0,i-1);
                str1 = str1.substr(i+1,str1.length-i);
                a = str1.substr(0,2);
                str1 = str1.substr(2,str1.length - 2);
                if(parseInt("0x" + a) & 0x80 == 0){
                    substr = substr + String.fromCharCode(parseInt("0x" + a));
                }
                else if(parseInt("0x" + a) & 0xE0 == 0xC0){ //two byte
                    b = str1.substr(1,2);
                    str1 = str1.substr(3,str1.length - 3);
                    var widechar = (parseInt("0x" + a) & 0x1F) << 6;
                    widechar = widechar | (parseInt("0x" + b) & 0x3F);
                    substr = substr + String.fromCharCode(widechar);
                }
                else{
                    b = str1.substr(1,2);
                    str1 = str1.substr(3,str1.length - 3);
                    c = str1.substr(1,2);
                    str1 = str1.substr(3,str1.length - 3);
                    var widechar = (parseInt("0x" + a) & 0x0F) << 12;
                    widechar = widechar | ((parseInt("0x" + b) & 0x3F) << 6);
                    widechar = widechar | (parseInt("0x" + c) & 0x3F);
                    substr = substr + String.fromCharCode(widechar);
                }
            }
            else {
                substr = substr + str1.substring(0,i);
                str1= str1.substring(i);
            }
            i = str1.indexOf("%");
        }

        return substr+str1;
    }
}


function bluetoothWrite() {
    var cmd = new _EscCommand(true);
    var content = cmd.TextAlignLeft + new GB2312UTF8().Utf8ToGb2312("打印测试");
    content += "\n\n\n\n";
    content += cmd.CutAllPage;
    bluetooth.write(function(){
        document.getElementById("writeInfo").innerHTML = "打印小票成功";
    },function(){
        document.getElementById("writeInfo").innerHTML = "打印小票失败";
    }, content,"Gb2312", true);
}

function bluetoothDisConnect() {
    bluetooth.disconnect(function(){
        document.getElementById("disconnectInfo").innerHTML = "关闭成功";
    },function(){
        document.getElementById("disconnectInfo").innerHTML = "关闭失败";
    });
}

function stopConnectionManager() {
    bluetooth.stopConnectionManager(function(){
        document.getElementById("stopConnectManageInfo").innerHTML = "可以关闭";
    },function(){
        document.getElementById("stopConnectManageInfo").innerHTML = "不可关闭";
    });
}




